package com.bee.web.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bee.web.annotation.ValidateToken;
import com.bee.web.service.ServiceAreaService;
import com.bee.web.util.FastJsonUtil;
import com.bee.web.util.Page;
import com.bee.web.vo.BaseVo;
import com.bee.web.vo.bean.AreaBean;

/**
 * 服务区域管理
 * 
 * @author liuzy
 * @version 2015年6月13日
 */
@Controller
@RequestMapping("/serviceArea")
public class ServiceAreaController {

    private Logger logger = Logger.getLogger(ServiceAreaController.class);

    @Autowired
    private ServiceAreaService serviceAreaService;

    @ValidateToken
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo search(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestBody(required = false) AreaBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        try {
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseVo("fail", "服务器异常");
    }

    @ValidateToken
    @RequestMapping(value = "/switchStatus", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo switchStatus(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestParam(required = false) String areaId,
            @RequestParam(required = false) String status,
            @RequestBody(required = false) AreaBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        try {
            serviceAreaService.switchStatus(areaId, status);
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseVo("fail", "服务器异常");
    }

    @ValidateToken
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo delete(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestParam(required = false) String areaId,
            @RequestBody(required = false) AreaBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        try {
        	if (areaId.contains("_")) {
				String[] ids = areaId.split("_");
				for (String id : ids) {
					serviceAreaService.delete(id);
				}
			} else {
				serviceAreaService.delete(areaId);
			}
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseVo("fail", "服务器异常");
    }
    
    @ValidateToken
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo saveOrUpdate(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestBody(required = false) AreaBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
    	try {
    		serviceAreaService.saveOrUpdate(bean);
            BaseVo vo = reloadData(page, rows, new AreaBean());
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
		} catch (Exception e) {
            e.printStackTrace();
		}
        return new BaseVo("fail", "服务器异常");
    }
    
    @ValidateToken
    @RequestMapping(value = "/isExists", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo isExists(@RequestParam(required = false) String areaId) {
    	logger.info("[参数]" + FastJsonUtil.toJSON(areaId));
    	try {
    		BaseVo vo = null;
    		if (serviceAreaService.isExists(areaId)) {
    			vo = new BaseVo("yes", "区域ID已存在");
			} else {
				vo = new BaseVo("no", "没有此区域ID");
			}
    		logger.info("[返回]" + FastJsonUtil.toJSON(vo));
    		return vo;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return new BaseVo("fail", "服务器异常");
    }
    
    private BaseVo reloadData(int page, int rows, AreaBean bean) throws Exception {
    	Page pager = new Page(page, rows);
    	int total = serviceAreaService.searchCount(bean, pager);
        List<AreaBean> list = serviceAreaService.searchList(bean, pager);
        BaseVo vo = new BaseVo(page, rows);
        vo.setData(list);
        vo.setCount(list.size());
        vo.setTotalCount(total);
        return vo;
    }

}
